package com.huluobo.a2307douyindemo.search

import androidx.lifecycle.MutableLiveData
import com.huluobo.lib_base.bean.SearchBean
import com.huluobo.lib_base.bean.VideoBean
import com.huluobo.lib_base.db.DouYinDatabase
import com.huluobo.lib_base.mvvm.BaseViewModel
import com.huluobo.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 *  Created by LC on 2025/9/24.
 */
@HiltViewModel
class SearchViewModel @Inject constructor(private val apiService: ApiService, private val db: DouYinDatabase) :
    BaseViewModel() {
    val getSearchSuccess by lazy { MutableLiveData<List<VideoBean>>() }
    fun searchVideo(keyword: String) {
        executeNetwork(
            { apiService.search(keyword) },
            getSearchSuccess::postValue
        )
    }

    val querySearchSuccess by lazy { MutableLiveData<List<SearchBean>>() }
    fun querySearch() {
        executeDb(
            { db.getSearchDao().queryAllSearch() },
            querySearchSuccess::postValue
        )
    }

    val saveSearchSuccess by lazy { MutableLiveData<String>() }
    fun saveSearch(searchBean: SearchBean) {
        executeDb(
            {
                db.getSearchDao().insertSearch(searchBean)
                "保存搜索记录成功"
            },
            saveSearchSuccess::postValue
        )
    }

    val deleteSearchSuccess by lazy { MutableLiveData<String>() }
    fun deleteSearch(searchBean: SearchBean) {
        executeDb(
            {
                db.getSearchDao().deleteSearch(searchBean)
                "删除搜索记录成功"
            },
            deleteSearchSuccess::postValue
        )
    }
}